package cn.cdeden.knowledge.controller;



import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.utils.StringUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.knowledge.domain.EnterpriseFilesTree;
import cn.cdeden.knowledge.domain.PortalConfigurationEnum;
import cn.cdeden.knowledge.domain.SysPortalConfigure;
import cn.cdeden.knowledge.service.EnterpriseFilesTreeService;
import cn.cdeden.knowledge.service.FilesLabelService;
import cn.cdeden.knowledge.service.SysPortalConfigureService;
import cn.cdeden.knowledge.service.dto.SysPortalConfigureDto;
import cn.cdeden.knowledge.service.dto.SysPortalConfigureQueryParam;
import cn.cdeden.knowledge.vo.EnterpriseFilesTreeVO;
import cn.cdeden.knowledge.vo.FilesLabelQueryVO;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
* @author gjx
* @date 2024-09-24
**/

/**
 * 企业门户相关的接口
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/sysPortalConfigure")
public class SysPortalConfigureController {

    private final SysPortalConfigureService sysPortalConfigureService;

    private final EnterpriseFilesTreeService enterpriseFilesTreeService;

    private final FilesLabelService filesLabelService;

    @GetMapping
    public TableDataInfo<SysPortalConfigure> query(SysPortalConfigureQueryParam query, Pageable pageable){
        String libraryId = query.getLibraryId();
        if (libraryId == null|| StringUtils.isNullOrBlank(libraryId))return new TableDataInfo<>();
        List<SysPortalConfigure> sysPortalConfigureList = sysPortalConfigureService.list();
        if (sysPortalConfigureList.isEmpty()) {
            List<SysPortalConfigure> defaultConfigureList = new ArrayList<>();
            for (PortalConfigurationEnum portalConfigurationEnum : PortalConfigurationEnum.values()) {
                defaultConfigureList.add(portalConfigurationEnum.toSysPortalConfigure());
            }
            sysPortalConfigureService.saveBatch(defaultConfigureList);
        }

        TableDataInfo<SysPortalConfigure> info = sysPortalConfigureService.queryAll(query,pageable);
        List<SysPortalConfigure> sysPortalConfigureDtoList = info.getRows().stream().sorted(Comparator.comparing(SysPortalConfigure::getCreateTime).reversed()).collect(Collectors.toList());
        info.setRows(sysPortalConfigureDtoList);
        return info;
    }

    @PostMapping
    public R create(@Validated @RequestBody SysPortalConfigureDto resources){
        return R.ok(sysPortalConfigureService.insert(resources));
    }

    @PutMapping
    public R update(@Validated @RequestBody SysPortalConfigureDto resources){
        return R.ok(sysPortalConfigureService.updateById(resources));
    }

    @DeleteMapping
    public R delete(@RequestBody Set<Long> ids) {
        return R.ok(sysPortalConfigureService.deleteByIds(ids));
    }

    @GetMapping("/get-all-portal")
//    @PreAuthorize("@el.check('sysPortalConfigure:list')")
    public R<List<SysPortalConfigure>> getAllPortal(String libraryId) {
        List<SysPortalConfigure> sysPortalConfigureList = sysPortalConfigureService.lambdaQuery().eq(SysPortalConfigure::getLibraryId,libraryId).list();
/*
        if (sysPortalConfigureList.isEmpty()) {
            List<SysPortalConfigure> defaultConfigureList = new ArrayList<>();
            for (PortalConfigurationEnum portalConfigurationEnum : PortalConfigurationEnum.values()) {
                defaultConfigureList.add(portalConfigurationEnum.toSysPortalConfigure());
            }
            defaultConfigureList.forEach(action->action.setLibraryId(libraryId));
            sysPortalConfigureService.saveBatch(defaultConfigureList);
        }
*/

        return R.ok(sysPortalConfigureList);
    }

    @GetMapping("/get-portal-file/{id}")
    public R<EnterpriseFilesTreeVO> getPortalFile(@PathVariable("id") Long fileId) {
//        enterpriseFilesTreeService.getPortalFile(fileId);
//        List<String> fileIds = new ArrayList<>();
//        fileIds.add(fileId.toString());
//        List<EnterpriseFilesTree> enterpriseFilesTrees = enterpriseFilesTreeService.selectJuniorAll(fileIds, "1");
        return R.ok(enterpriseFilesTreeService.getPortalFile(fileId));
    }

    @GetMapping("/get-enterprise-label")
//    @PreAuthorize("@el.check('sysPortalConfigure:list')")
    public R<List<FilesLabelQueryVO>> getEnterpriseLabel() {
        List<FilesLabelQueryVO> enterpriseLabel = filesLabelService.getEnterpriseLabel();
//        //递归组建标签的树形目录
//        List<FilesLabel> filesLabelList = filesLabelService.list(new QueryWrapper<FilesLabel>().eq("label_type", "sys"));
//        List<FilesLabelQueryVO> filesLabelQueryVOList = ConvertUtil.convertList(filesLabelList, FilesLabelQueryVO.class);
//        //找出所有最顶层的标签
//        List<FilesLabel> filesLabelLists = filesLabelList.stream().filter(filesLabel -> filesLabel.getParentId().equals(0L)).collect(Collectors.toList());
//        List<FilesLabelQueryVO> filesLabelQueryVOList1 = ConvertUtil.convertList(filesLabelLists, FilesLabelQueryVO.class);
//        filesLabelQueryVOList1.forEach(v -> {
//            setChildren(v, filesLabelQueryVOList);
//        });
        return R.ok(enterpriseLabel);
    }

//    public void setChildren(FilesLabelQueryVO filesLabelQueryVO, List<FilesLabelQueryVO> filesLabelQueryVOList) {
//        //初始化子数据
//        filesLabelQueryVO.setChildFilesLabelList(new ArrayList<>());
//        for (FilesLabelQueryVO filesLabelQueryVO1 : filesLabelQueryVOList) {
//            if (filesLabelQueryVO1.getParentId() != null && filesLabelQueryVO1.getParentId().equals(filesLabelQueryVO.getId())) {
//                filesLabelQueryVO.getChildFilesLabelList().add(filesLabelQueryVO1);
//                setChildren(filesLabelQueryVO1, filesLabelQueryVOList);
//            }
//        }
//    }

    /*
    @Log("导出SysPortalConfigureService")
    @ApiOperation("导出SysPortalConfigureService")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('sysPortalConfigure:list')")
    public void download(HttpServletResponse response, SysPortalConfigureQueryParam query) throws IOException {
        sysPortalConfigureService.download(sysPortalConfigureService.queryAll(query), response);
    }*/

}
